package com.whz.workmanageol.controller;

import com.whz.workmanageol.entity.Publish;
import com.whz.workmanageol.entity.User;
import com.whz.workmanageol.service.StudentService;
import com.whz.workmanageol.vo.PublishWorkVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);
    @Autowired
    private StudentService studentService;

    @GetMapping(value = "publishlist")
    public String publishlist(HttpServletRequest request, Model model){
        String courseid = request.getParameter("courseid");
        String gradeid = request.getParameter("gradeid");
        String studentid = ((User)request.getSession().getAttribute("user")).getId();
        List<PublishWorkVO> publishWorkVOList = studentService.findPublish(studentid, courseid, gradeid);
        model.addAttribute("publishWorkVOList", publishWorkVOList);
        return "bootstrap/publishliststu";
    }

    @PostMapping("submit")
    public String submit(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam("file") MultipartFile file,
                       String publishid,
                       String workid){

        if(!studentService.couldSubmit(publishid)){
            throw new IllegalArgumentException("作业已截至");
        }
        // 替换本地作业文件
        studentService.replaceWork(file, workid);
        // 更新作业提交时间
        studentService.saveWorkSubmitDate(workid);
        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('作业已上交完毕,请关闭该弹窗并刷新发布页面！');</script>");
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "bootstrap/index";
    }
    @RequestMapping("/showPublish")
    public String showPublish(HttpServletRequest request, Model model){
        String publishid = request.getParameter("publishid");
        Publish publish = studentService.findPublishById(publishid);
        model.addAttribute("publish", publish);
        return "bootstrap/showpublish";
    }

    @RequestMapping("/downAttach")
    public void downAttach(HttpServletRequest request, HttpServletResponse response){
        String publishid = request.getParameter("publishid");
        String filename = studentService.downAttach(publishid, response);
        // 设置信息给客户端不解析
        String type = new MimetypesFileTypeMap().getContentType(filename);
        response.setHeader("Content-type", type);
        //F__WorkManageOL_387daa37-958b-4be9-9cf3-1414658b013b_55675819-da9a-4fe3-8524-a79731edb252_9eab4458-d63c-44a2-8014-4bd854045024_5_publish_5
        String encode = new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        String attachName = "attachNo." + encode.substring(encode.lastIndexOf(".") - 1) ;
        response.setHeader("Content-Disposition", "attachment;filename=" + attachName);
    }

    @GetMapping("/queryWorkScore/{workid}")
    @ResponseBody
    public String queryWorkScore(@PathVariable(name = "workid") String workid) {
        return studentService.queryWorkScore(workid);
    }
}
